package com.ljl.mvvmdemo.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.ljl.mvvmdemo.db.dao.LoginDao
import com.ljl.mvvmdemo.db.dao.PosMachineDao
import com.ljl.mvvmdemo.db.entity.Login
import com.ljl.mvvmdemo.db.entity.PosMachine
import com.ljl.mvvmdemo.getApplication

/**
 * Room数据库
 * created by luojialun on 2024/9/4
 */
@Database(entities = [Login::class, PosMachine::class], version = 1, exportSchema = true)
abstract class AppDatabase : RoomDatabase() {

    abstract fun getLoginDao(): LoginDao
    abstract fun getPosMachineDao(): PosMachineDao

    companion object {
        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context, AppDatabase::class.java, "ljl-db")
                .addMigrations().build()
        }
    }

    /* val MIGRATION_1_2 = object : Migration(1, 2) {
         override fun migrate(database: SupportSQLiteDatabase) {
             // database.execSQL("ALTER TABLE SaleParam ADD COLUMN ticket_print_trace_barcode INTEGER DEFAULT 0")
         }
     }*/

}

fun getAppDataBase(): AppDatabase {
    return AppDatabase.getInstance(getApplication())
}